package com.app.business.service;

import com.app.business.model.Contract;
import com.app.business.model.House;
import com.app.business.model.YxHouse;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预选数据保存
 * Created by szy on 2024/6/21
 */
@SuppressWarnings(value = "ALL")
@Service
public class YxHouseService extends JpaBaseDao<YxHouse,Integer> {
    @Autowired
    private ContractService contractService;
    @Autowired
    private HouseService houseService;

    @Transactional
    public synchronized Response chooseHouseYxSave(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
        Integer displaceStatus = ParamUtils.getInt(request, "displaceStatus", 0);
        BigDecimal displaceArea = ParamUtils.getDecimal(request, "displaceArea", 0);

        House house = this.houseService.findOne(houseId);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(house != null && contract != null, "1111", "数据错误！");

        List<Integer> yxHouseIdList = this.getHouseIdByContractId(contract.getId());
        if (!yxHouseIdList.contains(houseId) && houseId != 0 && contract.getId() != 0){//该户不在预选中
            YxHouse yxHouse = new YxHouse();
            yxHouse.setHouseId(houseId);
            yxHouse.setContractId(contract.getId());
            this.save(yxHouse);
        }

        Map map = Maps.newHashMap();
        map.put("zsProjectId", contract.getZsProject().getId());
        map.put("azProjectId", house.getAzProject().getId());
        map.put("buildNum", house.getBuildNum());
        map.put("contractId", contract.getId());
        Response response = new Response();
        response.setData(map);
        response.setCode("0001");
        response.setMessage("保存成功！");
        return response;
    }

    /**
     * 预选房源面积
     * @param contractId
     * @return
     */
    public BigDecimal getYxHouseArea(Integer contractId) {
        StringBuffer sql = new StringBuffer(" SELECT SUM(IFNULL(h.property_area,0) + IFNULL(h.pool_area,0)) AS yxHouseArea ");
        sql.append(" FROM yx_house yx,house h  ");
        sql.append(" WHERE yx.house_id = h.id AND yx.contract_id = :contractId ");
        Map<String, Object> map = new HashMap<>();
        map.put("contractId", contractId);
        return BigDecimalUtils.stringToBigDecimal(String.valueOf(this.findSqlObject(sql.toString(), map).get(0)));
    }


    /**
     * 得到contractId对应下的houseidList
     * @param contractId
     * @return
     */
    public List<Integer> getHouseIdByContractId(Integer contractId) {
        Map<String, Object> map = Maps.newLinkedHashMap();
        StringBuffer sql = new StringBuffer("select yx.house_id ");
        sql.append("from yx_house yx  where yx.contract_id = :contractId  ");
        map.put("contractId", contractId);
        return (List<Integer>) this.findSqlObject(sql.toString(), map);
    }

    /**
     * 删除预选方案
     * @param houseId
     * @param contractId
     */
    @Transactional
    public synchronized void deleteYxHouse(Integer houseId,Integer contractId) {
        String sql = "delete from yx_house  where house_id =:houseId and contract_id =:contractId";
        this.entityManager.createNativeQuery(sql).setParameter("houseId", houseId).setParameter("contractId",contractId).executeUpdate();
    }

    /**
     * 预选houseList
     * @param contractId
     * @return
     */
    public List<House> getYxHouseList(Integer contractId) {
        StringBuffer hql = new StringBuffer(" from House h  where h.id ");
        hql.append(" in (select yx.houseId from YxHouse yx where yx.contractId =:contractId)");
        List<House> houseList = entityManager.createQuery(hql.toString()).setParameter("contractId", contractId).getResultList();

        return houseList;
    }



}
